package cn.cms.controller;

import cn.cms.annotation.SystemLogAnno;
import cn.cms.dao.PageDao;
import cn.cms.model.Rank;
import cn.cms.service.RankService;
import cn.cms.util.ResponseUtil;
import cn.cms.webcommon.ResBo;
import com.alibaba.fastjson.JSONObject;
import org.jfaster.mango.transaction.Transaction;
import org.jfaster.mango.transaction.TransactionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;


@Controller
@RequestMapping(value = "rank")
public class RankController extends AbstractController {

    @Autowired
    private RankService rankService;

    /**
     * 列表头
     */
    @RequestMapping(value = "/page", method = RequestMethod.POST)
    public String page(String enabledPro, Model model) {
        model.addAttribute("enabledPro", enabledPro);
        return "rank/page";
    }

    /**
     * 列表页
     */
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public String list(HttpServletRequest request, PageDao<Rank> pageDao,
                       Rank rank,
                       // 路径产品标识
                       String enabledPro,
                       Model model) {
        PageDao<Rank> pages = rankService.getListPage(pageDao, rank);
        model.addAttribute("pages", pages);

        model.addAttribute("enabledPro", enabledPro);

        // 将查询参数传递给服务组件
        model.addAttribute("divId", request.getParameter("divId"));
        Map<String, Object> paramMap = super.getRequestMap(request);
        JSONObject json = JSONObject.parseObject(JSONObject.toJSONString(paramMap));
        model.addAttribute("queryCondition", json.toString());
        return "rank/list";
    }

    /**
     * 加载添加，修改页面
     */
    @RequestMapping(value = "/editPage", method = RequestMethod.POST)
    public String editPage(HttpServletResponse response, Rank rank, Model model) {
        if (rank.getId() != null) {
            rank = rankService.getById(rank.getId());
            model.addAttribute("rank", rank);
        } else {
            model.addAttribute("create", 1);
        }
        return "rank/form";
    }

    /**
     * 保存排序
     */
    @RequestMapping(value = "/saveSort", method = RequestMethod.POST)
    @ResponseBody
    @SystemLogAnno(operationType = SystemLogAnno.UPD, operationName = " 排行榜排序-排行榜管理")
    public void saveSort(HttpServletResponse response, Long[] sortIds, Integer pageSizeNo, Integer pageNo) {
        int index = (pageNo - 1) * pageSizeNo + 1;
        for (Long id : sortIds) {
            // 查询
            Rank rank = rankService.getById(id);
            // 重置
            rank.setColSort(index);
            // 更新
            rankService.update(rank);
            // 排序号+1
            index++;
        }
        ResBo resBo = new ResBo();
        ResponseUtil.printAjaxPack(response, resBo);
    }

    /**
     * 保存
     */
    @RequestMapping(value = "save", method = RequestMethod.POST)
    @ResponseBody
    @SystemLogAnno(operationType = SystemLogAnno.ADDORUPD, operationName = " 排行榜-排行榜管理")
    public void save(HttpServletResponse response, Rank rank) {
        Rank rankToSave;
        if (rank.getId() == null) {
            rankToSave = rank;
            rankService.icrSortAll();
            rankToSave.setColSort(1);
            rankToSave.setRankStatus(Rank.RANK_STATUS_UNUSE);
            rankService.save(rankToSave);
        } else {
            rankToSave = rankService.getById(rank.getId());
            rankToSave.setColName(rank.getColName());
            rankToSave.setColSubName(rank.getColSubName());
            rankToSave.setColPoster(rank.getColPoster());
            rankToSave.setColVideoType(rank.getColVideoType());
            rankToSave.setOutsideNum(rank.getOutsideNum());
            rankToSave.setInsideNum(rank.getInsideNum());
            rankToSave.setRobotFlag(rank.getRobotFlag());
            rankToSave.setColState(rank.getColState());
            rankToSave.setEnabledPro(rank.getEnabledPro());
            rankToSave.setPlatForm(rank.getPlatForm());
            rankService.update(rankToSave);
        }
        ResBo resBo = new ResBo();
        ResponseUtil.printAjaxPack(response, resBo);
    }

    /**
     * 删除
     */
    @RequestMapping(value = "/del", method = RequestMethod.POST)
    @ResponseBody
    @SystemLogAnno(operationType = SystemLogAnno.DEL, operationName = " 排行榜-排行榜管理")
    public void del(HttpServletResponse response, Rank rank) {
        ResBo resBo = new ResBo();
        if (rank.getId() != null) {
            Transaction tx = TransactionFactory.newTransaction();
            try {
                rankService.del(rank);
                tx.commit();
            } catch (Exception e) {
                tx.rollback();
                logger.error("", e);
                throw new RuntimeException(e);
            }
        } else {
            resBo.setSuccess(false);
            resBo.setErrMsg("该项不存在或已经删除！");
        }
        ResponseUtil.printAjaxPack(response, resBo);
    }

    /**
     * 修改发布状态
     */
    @RequestMapping(value = "/chgState", method = RequestMethod.POST)
    @ResponseBody
    @SystemLogAnno(operationType = SystemLogAnno.UPD, operationName = " 排行榜状态-排行榜管理")
    public void chgState(HttpServletResponse response, Long id, Integer colState) {

        Rank rank = rankService.getById(id);
        if (rank != null) {
            rank.setColState(colState);
            rankService.update(rank);
        }

        ResBo resBo = new ResBo();
        ResponseUtil.printAjaxPack(response, resBo);
    }

}
